﻿using System;
using System.Collections.Generic;
using System.Linq;
using MovieAPI.Models;
namespace MovieAPI.DAL
{
    public class MovieInfoDAL
    {

        /// <summary>
        /// 查询所有电影信息
        /// </summary>
        /// <returns></returns>
        public static List<MovieInfo> GetAllMovie()
        {
            using (filmEntities db = new filmEntities())
            {
                var asd = db.MovieInfo.ToString();
                return db.MovieInfo.ToList();
            }

        }
        public static List<MovieInfo> GetComingMovie()
        {
            using (filmEntities db = new filmEntities())
            {
                
                var movie = db.MovieInfo.Where(m => m.MovieDate > System.DateTime.Now).ToList();
                return movie;
            }

        }
        public static List<MovieInfo> GetOnMovie()
        {
            using (filmEntities db = new filmEntities())
            {
                int[] arr = db.Schedule.Where(S => S.StartTime > DateTime.Now).Select(t => t.MovieID).ToArray();
                var Movie1 = new List<MovieInfo>();
                foreach (var item in arr)
                {
                    Movie1.Add(db.MovieInfo.Where(w => w.MovieID == item).FirstOrDefault());
                }
                var result=Movie1.Where((x, i) => Movie1.FindIndex(z => z.MovieID == x.MovieID) == i).ToList();
                return result;
            }

        }
        public static List<string> GetMovieType()
        {
            using (filmEntities db = new filmEntities())
            {
                var Movie = db.MovieInfo.Select(T => T.MovieType).ToArray();
                List<string> MovieType = new List<string>();
                char[] temp = { ',', '，' };
                foreach (var item in Movie)
                {
                    var Type = item.Split(temp);
                    foreach (var Ty in Type)
                    {
                        MovieType.Add(Ty);
                    }

                }
                return MovieType.Where((x, i) => MovieType.FindIndex(z => z == x) == i).ToList();
            }

        }
        public static List<string> GetMovieCon()
        {
            using (filmEntities db = new filmEntities())
            {
                var Movie = db.MovieInfo.Select(T => T.MovieAddress).ToArray();
                List<string> MovieCon = new List<string>();

                foreach (var item in Movie)
                {
                    MovieCon.Add(item);
                }
                return MovieCon.Where((x, i) => MovieCon.FindIndex(z => z == x) == i).ToList();
            }
        }
        public static List<string> GetMovieYear()
        {
            using (filmEntities db = new filmEntities())
            {
                var Movie = db.MovieInfo.Select(T => T.MovieDate).ToArray();
                List<string> MovieCon = new List<string>();

                foreach (var item in Movie)
                {
                    MovieCon.Add(item.ToString().Substring(0, 4));
                }
                var MovieYear = MovieCon.Where((x, i) => MovieCon.FindIndex(z => z == x) == i).ToArray();
                Array.Sort(MovieYear);
                Array.Reverse(MovieYear);
                return MovieYear.ToList();
            }
        }
        
        public static List<MovieInfo> GetMovieByType(string MovieType, int? MovieYear, string MovieCon,string MovieIndex)
        {
            using (filmEntities db=new filmEntities())
            {
                List<MovieInfo> comingMovie = new List<MovieInfo>();
                if (MovieIndex == "2")
                {
                   comingMovie = GetComingMovie();
                    
                }
                else if (MovieIndex == "1")
                {
                  comingMovie = GetOnMovie();
                }
                else
                {
                    comingMovie = GetAllMovie();
                }
                return comingMovie.Where(M =>
                (string.IsNullOrEmpty(MovieType) ?true:M.MovieType.Contains(MovieType))
                &&(string.IsNullOrEmpty(MovieYear.ToString())?true:M.MovieDate.Year==MovieYear)
                &&(string.IsNullOrEmpty(MovieCon)?true:M.MovieAddress==MovieCon)
                ).ToList();
            }
        }
    }
}